package com.saasitems.admin.web.controller;


import com.saasitems.admin.service.MediaResService;
import com.saasitems.admin.web.vo.matts.UpMattsVO;
import com.saasitems.common.utils.DateUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by Administrator on 2016/4/17.
 */
@RestController
public class MediaResController {

    private static final Logger logger = Logger.getLogger(MediaResController.class);

    @Autowired
    private MediaResService mediaResService;

//    @RequestMapping(value="/upload/{mattsId}", method= RequestMethod.POST)
    @RequestMapping(value="mediares/upload", method= RequestMethod.POST)
    public UpMattsVO upload(HttpServletRequest request) throws IOException {

        String fname = "";
        UpMattsVO o = new UpMattsVO();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Iterator<String> fileNames = multipartRequest.getFileNames();

        String resId = "";
        String originalFileName = "";
        while (fileNames.hasNext()) {
            MultipartFile myfile = multipartRequest.getFile(fileNames.next());
            String realPath = request.getSession().getServletContext().getRealPath("/upload");
            String yyyyMM = DateUtils.getYear() + DateUtils.getMonth();
            realPath = realPath + File.separator + yyyyMM;

            String extName  = getExtName(myfile.getOriginalFilename());
            String uuid = mediaResService.saveMediaRes(myfile.getOriginalFilename(), extName, myfile.getSize());
            if (StringUtils.isBlank(originalFileName)) {
                originalFileName = myfile.getOriginalFilename();
            } else {
                originalFileName = originalFileName + ";" + myfile.getOriginalFilename();
            }
            if (StringUtils.isBlank(resId)) {
                resId = uuid;
            } else {
                resId = resId + ";" + uuid;
            }

            if (StringUtils.isNotBlank(extName)) {
                fname = resId + "." + extName;
            } else {
                fname = resId;
            }
            FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, fname));
        }
        o.setResult("success");
        o.setFilesUUID(resId);
        o.setFilesName(originalFileName);
        return o;
    }

    /**
     * 取得文件后缀
     * @param fileName
     * @return
     */
    private String getExtName(String fileName) {
        final int offset = fileName.lastIndexOf(".");
        if (offset < 0) {
            return "";
        }
        return fileName.substring(offset +1);
    }
}
